//
//  VillageIntroManageVC.m
//  Cunli
//
//  Created by lilubin on 15/8/6.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "VillageIntroManageVC.h"
#import "ZYQAssetPickerController.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "Config.h"
#import "UIViewExt.h"
#import "HGPhotoWall.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "UIImage+fixOrientation.h"
#import "Utils.h"
#import "MWPhotoBrowser.h"
//#import "MBProgressHUD.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define imgViewTag 300
#define closeBtnTag 500
//#define PICBASEURL @"http://121.41.117.249"
#define PICBASEURL @"http://cunli.zhanyaa.com"



@interface VillageIntroManageVC ()<ZYQAssetPickerControllerDelegate,UINavigationControllerDelegate,HGPhotoWallDelegate, UIActionSheetDelegate,UIImagePickerControllerDelegate, MWPhotoBrowserDelegate, UITextViewDelegate>{
    
    float screenWidth,screenHeight;
    //    UITextField *titleField;
    UITextView *contentView;
    UIButton *addPicBtn;           //添加本地图片按钮
    NSInteger lastArrayCount;
    NSMutableArray *imgDataArray;  //存储将要上传的图片的数据
    NSArray *picArray;
    NSString *urlString;
    NSMutableString *picsUrlString;
    UIImageView *tongZhiImg,*tongGaoImg;
    UILabel *_contentLabel;
    CGFloat _picWidth;
    MBProgressHUD *picHud;
    //    NSMutableArray *_imgShowArray; //展示图片的imageView数组
    NSMutableArray *_imagesArray;
    NSString *_contentStr;
    NSMutableArray *_oldImageURLArrays; //存储原来上传过的图片
    UILabel *_preContentLabel;
//    MBProgressHUD *_hud;
}

@property (nonatomic, strong) HGPhotoWall *photoWall;
@property (nonatomic, strong) NSMutableArray *photos;

@end

@implementation VillageIntroManageVC

- (void)viewDidLoad {
#warning 此处的基地址需要修改
    [super viewDidLoad];
    [self initGloableParams];
    [self loadIntroduceData];
    [self createNavBtn];
    [self createView];
    //    [self createAddPicBtn];
}

- (void)initGloableParams
{
    _picWidth=(kScreenWidth-85)/4.0;
    _oldImageURLArrays = [[NSMutableArray alloc] initWithCapacity:1];
//    self.view.backgroundColor = [UIColor whiteColor];
    imgDataArray=[[NSMutableArray alloc]init];
    screenWidth=[UIScreen mainScreen].bounds.size.width;
    screenHeight=[UIScreen mainScreen].bounds.size.height;
    _photos = [NSMutableArray array];
}

- (void)createNavBtn
{
    self.title=@"本村介绍发布";
    /*
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 28, 28);
    [leftBtn setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    */
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 65, 30);
    [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(publishThings) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpacer.width = flexSpacer.width - 20;
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:flexSpacer, rightBarItem, nil]];
}

- (instancetype)init
{
    if (self = [super init]) {
        //        _imgShowArray = [NSMutableArray arrayWithCapacity:1];
    }
    return self;
}

- (void)loadIntroduceData
{
    
    __weak typeof(self) weakSelf = self;
    _imagesArray = [NSMutableArray arrayWithCapacity:1];
    NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:3];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:[Config getOwnAreaids] forKey:@"areaids"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    [MyDataService requestURL:@"getinfo_intro.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        NSLog(@"VillageIntroduceResult:%@", result);
        if ([result[@"response"] isEqualToString:@"getinfo_intro"]) {
            NSDictionary *dataDic = result[@"data"];
            _contentStr = dataDic[@"content"];
            contentView.text = _contentStr;
            if (_contentStr.length != 0) {
                _preContentLabel.hidden = YES;
            }
            NSArray *imgs = dataDic[@"big_pics"];
            if (imgs.count != 0) {
                if (imgs.count == 1 && [imgs[0] isEqualToString:[NSString stringWithFormat:@"%@http://1.jpg", PICBASEURL]])
                {
                    [_oldImageURLArrays removeAllObjects];
                }
                else
                {
                    for(NSString *picURL in imgs)
                    {
                        NSString *newPicURL = [picURL substringFromIndex:PICBASEURL.length];
                        MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:picURL]];
                        [self.photos addObject:photo];
                        [_oldImageURLArrays addObject:newPicURL];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
//                UIView *photoView = [[UIView alloc] initWithFrame:CGRectMake(60, 190, kScreenWidth, 400)];
                UIView *photoView = [[UIView alloc] initWithFrame:CGRectMake(18, 190, kScreenWidth , 400)];
                
                weakSelf.photoWall = [[HGPhotoWall alloc] initWithFrame:CGRectMake(0, 0, 0, 0) andLayoutPhotoType:LayoutTenPhotos];
                [weakSelf.photoWall setBackgroundColor:[UIColor colorWithHexString:UIBgColorValue]];
                if (imgs.count==1 && [imgs[0] isEqualToString:[NSString stringWithFormat:@"%@http://1.jpg", PICBASEURL]])
                {
                    [weakSelf.photoWall setPhotos:nil];
                }else
                {
                    [weakSelf.photoWall setPhotos:imgs];
                }
                weakSelf.photoWall.delegate = self;
                [weakSelf.photoWall setEditModel:YES];
                [photoView addSubview:_photoWall];
                photoView.userInteractionEnabled = YES;
                [weakSelf.view addSubview:photoView];
            });
            
        }
        
    } failureAction:^{
        
    }];
    
}

-(void)publishThings{
    
    if ([contentView.text isEqualToString:@""])
    {
        [Config showHUDWithText:@"内容不能为空" andImage:@"error"];
    }
    else if(contentView.text.length > 1000)
    {
        [Config showHUDWithText:@"字数超出限制" andImage:@"error"];
    }
    else
    {
        if (imgDataArray.count == 0)
        {
            for (int i=0; i<_oldImageURLArrays.count; i++)
            {
                if (i==0)
                {
                    NSString *picUrlString=[NSString stringWithFormat:@"%@",_oldImageURLArrays[0]];
                    picsUrlString=[NSMutableString stringWithString:picUrlString];
                }
                else
                {
                    NSString *picUrlString=[NSString stringWithFormat:@"%@",[_oldImageURLArrays objectAtIndex:i]];
                    [picsUrlString appendString:[NSString stringWithFormat:@",%@", picUrlString]];
                    
                }
            }
            
            [self sendContent];
        }
        else
        {
            [self uploadPics];
        }
    }
}

-(void)sendContent{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc] init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:contentView.text forKey:@"content"];
    [CheckParams setObject:[Config getOwnAreaids] forKey:@"areaids"];
    
    if (picsUrlString) {
        [CheckParams setObject:picsUrlString forKey:@"pics"];
        //        NSLog(@"----picsUrlString:%@----", picsUrlString);
    }else{
        [CheckParams setObject:@"http://1.jpg" forKey:@"pics"];
    }
    
    
    urlString=@"op_intropublish.php";
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    [MyDataService requestURL:urlString httpMethod:@"POST" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"%@",result);
        NSString *str=[[result objectForKey:@"error"] objectForKey:@"text"];
        if (str) {
            
            [picHud hide:YES];
            [Config showHUDWithText:@"发送失败" andImage:@"error"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            
            [picHud hide:YES];
            [Config showHUDWithText:@"发送成功" andImage:@"done"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }failureAction:^{
        NSLog(@"Error result");
        [picHud hide:YES];
        [Config showHUDWithText:@"发送失败" andImage:@"error"];
    }];
    
}

-(void)uploadPics{
    
    picHud= [Config createHUD];
    picHud.mode = 0;
    picHud.labelText = @"上传图片中...";
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc] init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    NSMutableDictionary *file=[[NSMutableDictionary alloc]init];
    [file setValue:imgDataArray forKey:@"image[]"];
    [MyDataService requestURL:@"upfile_imgs.php" httpMethod:@"POST" params:CheckParams fileDatas:file complection:^(id result) {
        
        NSData *data=result;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@", dic);
        NSDictionary *dataDic=[dic objectForKey:@"date"];
        picArray=[[NSArray alloc]initWithArray:[dataDic objectForKey:@"pics"]];
        
        if (!picArray.count==imgDataArray.count) {
            [picHud hide:YES];
            [Config showHUDWithText:@"上传图片失败" andImage:@"error"];
            return ;
        }
        
        for (int i=0; i<picArray.count; i++)
        {
            if (i==0) {
                NSString *picUrlString=[NSString stringWithFormat:@"%@",[picArray objectAtIndex:i]];
                picsUrlString=[NSMutableString stringWithString:picUrlString];
            }else{
                NSString *picUrlString=[NSString stringWithFormat:@",%@",[picArray objectAtIndex:i]];
                picsUrlString=[NSMutableString stringWithString:[NSString stringWithFormat:@"%@%@",picsUrlString,picUrlString]];
            }
        }
        
        if (_oldImageURLArrays.count != 0)
        {
            for(NSString *picURL in _oldImageURLArrays)
            {
                //                NSString *newPicURL = [picURL substringFromIndex:PICBASEURL.length];
                [picsUrlString appendString:[NSString stringWithFormat:@",%@", picURL]];
                //                NSLog(@"----picsUrlString----:%@", picsUrlString);
            }
        }
        
        //        NSLog(@">>>>>>>>>>>>%@",picsUrlString);
        [self sendContent];
    }failureAction:^{
        [picHud hide:YES];
        [Config showHUDWithText:@"上传图片失败" andImage:@"error"];
    }];
}

/*
 - (void)mergeAllPicURL
 {
 if (_oldImageURLArrays.count != 0)
 {
 for(NSString *picURL in _oldImageURLArrays)
 {
 //            NSString *newPicURL = [picURL substringFromIndex:PICBASEURL.length];
 [picsUrlString appendString:[NSString stringWithFormat:@",%@", picURL]];
 //            NSLog(@"----picsUrlString----:%@", picsUrlString);
 }
 }
 }
 */

-(void)goBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)createView{
    
    /*
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 10, 40, 30)];
    titleLabel.text=@"内容";
    titleLabel.textAlignment=0;
    [self.view addSubview:titleLabel];
    */
    
    contentView=[[UITextView alloc]initWithFrame:CGRectMake(15, 10, screenWidth-30, 140)];
    contentView.font=[UIFont systemFontOfSize:14];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.layer.borderColor = [UIColor colorWithHexString:TextViewBorderColorValue].CGColor;
    contentView.layer.borderWidth =0.5;
    contentView.layer.cornerRadius = 2.0;
    [self.view addSubview:contentView];
    [contentView setTextColor:[UIColor colorWithHexString:InfoCellTitleColorValue]];
    contentView.delegate = self;
    
    _preContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, screenWidth-50, 21)];
    _preContentLabel.text = @"本村介绍内容";
    [_preContentLabel setTextColor:[UIColor colorWithHexString:InfoCellIntroduceColorValue]];
    [_preContentLabel setFont:[UIFont systemFontOfSize:14]];
    [contentView addSubview:_preContentLabel];
    
    UILabel *promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(18, 150, screenWidth-70, 30)];
    promptLabel.text = @"图片上传(最多10张)";
    promptLabel.textColor = [UIColor colorWithHexString:InfoCellIntroduceColorValue];
    promptLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:promptLabel];
}

/*
-(void)createAddPicBtn{
    
    addPicBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    addPicBtn.frame=CGRectMake(60, 190, _picWidth, _picWidth);
    [addPicBtn setBackgroundImage:[UIImage imageNamed:@"addphoto.png"] forState:UIControlStateNormal];
    [addPicBtn addTarget:self action:@selector(addPics) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addPicBtn];
    
    _contentLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 270, 150, 20)];
    _contentLabel.text=@"最多上传九张图片";
    _contentLabel.textAlignment=0;
    [self.view addSubview:_contentLabel];
}
 */

-(void)addPics{
    
    ZYQAssetPickerController *picker = [[ZYQAssetPickerController alloc] init];
    picker.maximumNumberOfSelection = 10-_oldImageURLArrays.count-imgDataArray.count;
    NSLog(@"--还可以选择---:%ld", (long)picker.maximumNumberOfSelection);
    picker.assetsFilter = [ALAssetsFilter allPhotos];
    picker.showEmptyGroups=NO;
    picker.delegate=self;
    picker.selectionFilter = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        if ([[(ALAsset*)evaluatedObject valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypeVideo]) {
            NSTimeInterval duration = [[(ALAsset*)evaluatedObject valueForProperty:ALAssetPropertyDuration] doubleValue];
            return duration >= 5;
        } else {
            return YES;
        }
    }];
    [self presentViewController:picker animated:YES completion:NULL];
}

#pragma - mark AssetPickerDelegate
-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{
    
    //移除上次的图片
    for (int i=0; i<lastArrayCount+1; i++) {
        
        if (i==lastArrayCount) {
            addPicBtn.frame=CGRectMake(1000, 10000, 70, 70);
            break;
        }
        UIImageView *img=(UIImageView *)[self.view viewWithTag:imgViewTag+i];
        [img removeFromSuperview];
    }
    
    lastArrayCount=assets.count;
    for (int i=0; i<assets.count; i++) {
        
        //        CGFloat picWidthAndBlock = _picWidth+5;
        
        /*
         if (i==assets.count) {
         addPicBtn.frame=CGRectMake(60+picWidthAndBlock*(i%4),190+picWidthAndBlock*(i/4),_picWidth,_picWidth);
         break ;
         }*/
        
        ALAsset *asset=assets[i];
        
        //        UIImageView *imgview=[[UIImageView alloc] initWithFrame:CGRectMake(60+picWidthAndBlock*(i%4),190+picWidthAndBlock*(i/4),_picWidth,_picWidth)];
        //        imgview.contentMode=UIViewContentModeScaleAspectFill;
        //        imgview.clipsToBounds=YES;
        //        imgview.tag=imgViewTag+i;
        UIImage *tempImg=[UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
        if (tempImg.size.width>1024 && tempImg.size.height>1024) {
            tempImg = [Utils sizeCompressImageTo1024With:tempImg];
        }
        MWPhoto *photo = [MWPhoto photoWithImage:tempImg];
        [self.photos addObject:photo];
        //        [imgview setImage:tempImg];
        //        imgview.userInteractionEnabled = YES;
        //        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        //        closeBtn.frame = CGRectMake(_picWidth-17, 2, 15, 15);
        //        [closeBtn setImage:[UIImage imageNamed:@"closeBtn40"] forState:UIControlStateNormal];
        //        closeBtn.tag = closeBtnTag+i;
        //        [closeBtn addTarget:self action:@selector(deleteImgAction:) forControlEvents:UIControlEventTouchUpInside];
        //        [imgview addSubview:closeBtn];
        //
        //        [_imgShowArray addObject:imgview]; //将imgview添加到数组中，方便删除
        //
        //        [self.view addSubview:imgview];
        [self.photoWall addLocalPhoto:tempImg];
        
        NSData *data=UIImagePNGRepresentation(tempImg);
        //判断图片大小确定压缩比例
        if (data.length>1024*1024*2) {
            data=UIImageJPEGRepresentation(tempImg,0.2);
        }else{
            data=UIImageJPEGRepresentation(tempImg,0.5);
        }
        [imgDataArray addObject:data];
    }
    
    CGRect labelOldFrame = _contentLabel.frame;
    labelOldFrame.origin.y = addPicBtn.frame.origin.y+_picWidth+10;
    _contentLabel.frame = labelOldFrame;
    
}

/*
 - (void)deleteImgAction:(UIButton *)button
 {
 NSLog(@"Button tag:%li", button.tag-closeBtnTag);
 [_imgShowArray removeObject:(UIImageView *)[self.view viewWithTag:imgViewTag+button.tag-closeBtnTag]];
 for(UIImageView *imgView in _imgShowArray)
 {
 NSLog(@"ImageView tag:%li", (long)imgView.tag);
 }
 }
 */

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}

#pragma - mark HGPhoto Delegate
- (void)photoWallAddAction
{
    //    [self addPics];
    [[[UIActionSheet alloc] initWithTitle:@"添加图片"
                                 delegate:self
                        cancelButtonTitle:@"取消"
                   destructiveButtonTitle:nil
                        otherButtonTitles:@"拍照", @"从相册中选择", nil]
     
     showInView:self.view];
}

//删除图片执行的代理方法
- (void)photoWallPhotoTaped:(NSUInteger)index
{
    if (index < _oldImageURLArrays.count)
    {
        [_oldImageURLArrays removeObjectAtIndex:index];
    }
    else
    {
        NSUInteger index2 = index - _oldImageURLArrays.count;
        [imgDataArray removeObjectAtIndex:index2];
    }
    [self.photoWall deletePhotoByIndex:index];
    [self.photos removeObjectAtIndex:index];
}

//点击放大图片执行的代理方法
- (void)photoWallPhotoBrowser:(NSUInteger)index
{
    NSLog(@"点击第%ld张图片", index);
    //    NSMutableArray *photos = [NSMutableArray array];
    //    NSMutableArray *thumbs = [NSMutableArray array];
    //    MWPhoto *photo;
    BOOL displayActionButton = YES;
    BOOL displaySelectionButtons = NO;
    BOOL displayNavArrows = NO;
    BOOL enableGrid = YES;
    BOOL startOnGrid = NO;
    
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    browser.displayActionButton = displayActionButton;
    browser.displayNavArrows = displayNavArrows;
    browser.displaySelectionButtons = displaySelectionButtons;
    browser.alwaysShowControls = displaySelectionButtons;
    browser.zoomPhotosToFill = YES;
    browser.enableGrid = enableGrid;
    browser.startOnGrid = startOnGrid;
    browser.enableSwipeToDismiss = NO;
    browser.isScrollerPic = YES;
    [browser setCurrentPhotoIndex:index];
    [browser showNextPhotoAnimated:YES];
    [browser showPreviousPhotoAnimated:YES];
    
    [self.navigationController pushViewController:browser animated:YES];
}

#pragma mark - MWPhotoBrowser Delegate
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return _photos.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < _photos.count)
        return [_photos objectAtIndex:index];
    return nil;
}


#pragma mark - ActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == actionSheet.cancelButtonIndex) {
        return;
    } else if (buttonIndex == 1) {
        [self addPics];
    } else {
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告"
                                                                message:@"无法访问摄像头"
                                                               delegate:nil
                                                      cancelButtonTitle:@"取消"
                                                      otherButtonTitles: nil];
            
            [alertView show];
        } else {
            UIImagePickerController *imagePickerController = [UIImagePickerController new];
            imagePickerController.delegate = self;
            imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePickerController.allowsEditing = NO;
            imagePickerController.showsCameraControls = YES;
            imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
            imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];
            
            [self presentViewController:imagePickerController animated:YES completion:nil];
        }
    }
}

#pragma mark - UIPickerViewDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:NULL];
    
    UIImage *tempImg = info[UIImagePickerControllerOriginalImage];
    
    
    [self.photoWall addLocalPhoto:tempImg];
    
    UIImage *tempImgOrientation = [tempImg fixOrientation];
    
    if (tempImgOrientation.size.width>1024 && tempImgOrientation.size.height>1024) {
        tempImgOrientation = [Utils sizeCompressImageTo1024With:tempImgOrientation];
    }
    MWPhoto *photo = [MWPhoto photoWithImage:tempImg];
    [self.photos addObject:photo];
    
    NSData *data=UIImagePNGRepresentation(tempImgOrientation);
    //判断图片大小确定压缩比例
    if (data.length>1024*1024*2) {
        data=UIImageJPEGRepresentation(tempImgOrientation,0.2);
    }else{
        data=UIImageJPEGRepresentation(tempImgOrientation,0.5);
    }
    [imgDataArray addObject:data];
    
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    _preContentLabel.hidden = YES;
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
